<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{total}} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list" :default-sort="{prop:'timeOfEntry',order:'ascending'}">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column sortable label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{row}">
              {{ formOfEmploymentPlus(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column sortable label="入职时间" prop="correctionTime" />
          <!-- <el-table-column label="账户状态" /> -->
          <el-table-column label="操作" width="280">
            <template #default="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[2, 4, 6, 8]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </el-row>
      </el-card>
      <el-dialog title="新增部门" :visible.sync="showDialog" @close="resetForm">
        <EmpDialog @success="hSuccess" ref="empDialogRef" @close="showDialog=false" />
      </el-dialog>
    </div>
  </div>
</template>


<script>
import {getEmployeesPlus,DelEmployeesPlus} from '@/api/employees'
import EmployeesEnt from '@/constant/employees'
import EmpDialog from './empDialog.vue'
console.log(EmployeesEnt.hireType);
export default {
    data() {
        return {
            page: 1,
            size: 2,
            list: [],
            total: 0,
            showDialog: false
        };
    },
    created() {
        this.loadEmployeesPlus();
    },
    methods: {
      hDel(id){
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const res = await DelEmployeesPlus(id)
          if(this.list.length === 1){
            this.page--
            if(this.page <= 0){
              this.page = 1
            }
          }
          this.$message.success(res.message)
          this.loadEmployeesPlus()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      resetForm(){
        // 重置表单 表单是子组件
        this.$refs.empDialogRef.resetForm()
      },
      hSuccess(){
        this.total++
        const maxPage = Math.ceil(this.total / this.size)
        this.page = maxPage
        this.showDialog = false
        this.loadEmployeesPlus()
      },
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.page = 1;
            this.size = val;
            this.loadEmployeesPlus();
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.page = val;
            this.loadEmployeesPlus();
        },
        formOfEmploymentPlus(data) {
            return EmployeesEnt.hireType.find(item => item.id == data).value;
        },
        async loadEmployeesPlus() {
            try {
                const res = await getEmployeesPlus(this.page, this.size);
                console.log(res);
                this.list = res.data.rows;
                this.total = res.data.total;
            }
            catch (error) {
                console.log(error);
            }
        }
    },
    components: { EmpDialog }
}
</script>

